package com.naizhuai.Model.db.search;

import android.support.annotation.NonNull;

import com.naizhuai.Model.db.AppDatabase;
import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.Model.db.user.UserDao;
import com.naizhuai.Model.db.user.UserDataSourse;
import com.naizhuai.base.AppExecutors;

import java.util.List;

/**
 * @author：code_han
 * @date 2018/5/29
 */
public class LocalSearchData implements SearchDataSourse {
    private static volatile LocalSearchData INSTANCE;

    private @NonNull
    SearchHistoryDao mSearchDao;

    private @NonNull
    AppExecutors mAppExecutors;

    public static LocalSearchData getInstance() {
        if (INSTANCE == null) {
            synchronized (LocalSearchData.class) {
                if (INSTANCE == null) {
                    INSTANCE = new LocalSearchData();
                }
            }
        }
        return INSTANCE;
    }

    public LocalSearchData() {
        mAppExecutors = new AppExecutors();
        mSearchDao = AppDatabase.getInstance().SearchDao();
    }

    @Override
    public void getUserAsyn(String sid, GetSearchCallback callback) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                final SearchHistoryBean user = mSearchDao.searchUserBySid(sid);
                mAppExecutors.mainThread().execute(new Runnable() {
                    @Override
                    public void run() {
                        if (user != null) {
                            callback.onUserLoaded(user);
                        } else {
                            callback.onDataNotAvailable();
                        }
                    }
                });
            }
        };
        mAppExecutors.diskIO().execute(runnable);
    }

    @Override
    public void searchUserLimit(int limit, GetSearchCallback callback) {
        mAppExecutors.diskIO().execute(new Runnable() {
            @Override
            public void run() {
                List<SearchHistoryBean> searchs = mSearchDao.searchUserLimit(limit);
                mAppExecutors.mainThread().execute(new Runnable() {
                    @Override
                    public void run() {
                        if (searchs != null) {
                            callback.onSearchDataArray(searchs);
                        } else {
                            callback.onDataNotAvailable();
                        }
                    }
                });
            }
        });
    }


    @Override
    public void saveSearchData(SearchHistoryBean searchBean) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                mSearchDao.saveSearchHistory(searchBean);
            }
        };

        mAppExecutors.diskIO().execute(runnable);
    }


    @Override
    public void deleteSearchData(SearchHistoryBean bean) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                mSearchDao.deleteSearchData(bean);
            }
        };

        mAppExecutors.diskIO().execute(runnable);
    }


}
